import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import login from '../components/login.vue'
import homePage from '../components/homePage.vue'


//首頁.....
const firstPage=()=>import('../components/firstPage')

// 管理員頁面....
const UserInfoPage =()=>import('../components/User/UserInfo')
const updateUserInfo=()=>import('../components/User/UpdateUser')

// 歌手頁面.....
const roomTypeInfo=()=>import('../components/singer/roomTypeInfo')
const updateRoomType=()=>import('../components/singer/UpdateRoomType')

// 歌曲頁面.....
const buildingInfo=()=>import('../components/SongPage/buildingInfo')
const updateBuildingInfo=()=>import('../components/SongPage/UpdateBuilding')

// 歌單頁面
const goodsTypeInfo=()=>import('../components/SongListPage/goodsInfo')
const updateGoodsType=()=>import('../components/SongListPage/UpdateGoods')

// 用戶頁面
const goodsInfo=()=>import('../components/UserPage/goodsInfo')
const updateGoodsInfo=()=>import('../components/UserPage/UpdateGoodsInfo')


/**
 * @description 後台暫時做成這樣...
 * @date        2021/7/2 9:38
 * @author      danny 
 */
/*-------------------------------------------------------------------------------------- */

//  會員頁面
const memberInfo=()=>import('../components/memberPage/memberInfo')
const updateMemberInfo=()=>import('../components/memberPage/UpdateMemberInfo')

// 客房頁面
const roomInfo=()=>import('../components/roomPage/roomInfo')
const updateRoomInfo=()=>import('../components/roomPage/UpdateRoomInfo')

const player=()=>import('../components/playSongPage/MySongPlayer')


const listSong=()=>import('../components/ListSongPage/ListSongPage')
const updateListSong=()=>import('../components/ListSongPage/updateListSong')


const routerPush=VueRouter.prototype.push
VueRouter.prototype.push=function push(location){
  return routerPush.call(this,location).catch(error=>error)
}

Vue.use(VueRouter)

const routes = [
  {
    path:'',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path:'/homepage',
    component:homePage,
      children:[
        //播放頁面.....
        {
         path:'playerPage',
         component:player
        },
        //首頁..........
        {
          path:'firstPage',
          component:firstPage,
          //keepAlive:true
        },
       //用戶.....................
        {
        path:'userInfo',
        component: UserInfoPage,
        
      },
      {
        path:'updateUser',
        component:updateUserInfo
       },

       /**----------------------------------------------------------- */
       //歌手類型..........................
       {
         path:'roomTypeInfo',
         component:roomTypeInfo
       },
       {
         path:'updateRoomType',
         component:updateRoomType
       },

        /**----------------------------------------------------------- */
        //歌曲信息...
        {
          path:'buildingInfo',
          component:buildingInfo
        },
        {
          path:'updateBuildingInfo',
          component:updateBuildingInfo
        },

        /**---------------------------------------------------------------------- */
        //商品類行信息..
        {
          path:'goodsTypeInfo',
          component:goodsTypeInfo
        },
        {
          path:'updateGoodsTypeInfo',
          component:updateGoodsType
        },

        /**--------------------------------------------------------- */
        // 商品管理...
        {
          path:'goodsInfo',
          component:goodsInfo
        },
        {
          path:'updateGoodsInfo',
          component:updateGoodsInfo
        },
         /**--------------------------------------------------------- */
        // 會員管理...
        {
          path:'memberInfo',
          component:memberInfo
        },
        {
          path:'updateMemberInfo',
          component:updateMemberInfo
        },

        /**--------------------------------------------------------------- */
        {
          path:'roomInfo',
          component:roomInfo
        },
        {
          path:'updateRoomInfo',
          component:updateRoomInfo
        },
        
        /**----------------------------------------------------------------- */
        /**
         * @歌單管理轉去的頁面
         */
        {
          path:'listSong',
          component:listSong
        },
        {
          path:'updateListSong',
          component:updateListSong
        }
    ]
  }
  
]

const router = new VueRouter({
  mode: 'history',
  routes
})


//過濾器....
/**
 * @description 
 * @這里是把防止用戶直接進入主頁面
 */

import axios from 'axios'

router.beforeEach((to,from,next)=>{
  let token = JSON.parse(localStorage.getItem('userInfo'));
  //console.log(token);
  // 這里是如果去login,但 token 又不為空
  // 則先讓它去檢查
   if(to.path == '/login' && token==null ) {
      next()
   }
   else if(to.path=='/login' && token!=null){
    axios.get("http://localhost:8081/checkLogin?sessionId="+token.userSessionId)
    .then(res=>{
      console.log(res.data);
      if(res.data==true){
        next('/homepage/firstpage')
      }  
    })
   }
    else if(to.path!='/login' && token!=null){
    //假如是其它
     console.log(token);
      axios.get("http://localhost:8081/checkLogin?sessionId="+token.userSessionId)
      .then(res=>{
        console.log(res.data);
        if(res.data==false){
          localStorage.removeItem('userInfo')
          next('/login')
        }  
        //真的話繼續...   
        next() 
      })
    }
    else if(to.path!='/login' && token==null){
      next('/login');
    } 
    else{
      next();
    }
  }
)


export default router
